package com.pzb.web.controller.wechat;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pzb.market.domain.MkIntegralSetting;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.wechat.domain.WechatPickupSetting;
import com.pzb.wechat.service.IWechatPickupSettingService;

/**
 * 自提设置Controller
 * 
 * @author Mr_Wu
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/wechat/pickupSetting")
public class WechatPickupSettingController extends BaseController
{
    @Autowired
    private IWechatPickupSettingService wechatPickupSettingService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    @GetMapping
    public AjaxResult getInfo()
    {
        WechatPickupSetting pickupSetting = wechatPickupSettingService.selectWechatPickupSetting(wechatAppletService.getExtAppid());
        if(pickupSetting!=null){
            pickupSetting.setRegion(JSON.parseArray(pickupSetting.getAreaCode()));
        }
        return AjaxResult.success(pickupSetting);
    }

    /**
     * 修改自提设置
     */
    @Log(title = "自提设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WechatPickupSetting wechatPickupSetting)
    {
        wechatPickupSetting.setAreaCode(wechatPickupSetting.getRegion().toString());
        String fullAddress = "";
        for (Object address : wechatPickupSetting.getRegion()) {
            JSONObject a = (JSONObject) JSONObject.toJSON(address);
            fullAddress+= a.getString("label");
        }
        wechatPickupSetting.setFullAddress(fullAddress+wechatPickupSetting.getAddress());
        return toAjax(wechatPickupSettingService.updateWechatPickupSetting(wechatPickupSetting));
    }

    /**
     * 状态修改
     */
    @Log(title = "积分设置状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody WechatPickupSetting wechatPickupSetting)
    {
        return toAjax(wechatPickupSettingService.updateWechatPickupSetting(wechatPickupSetting));
    }


}
